//
//  ZYQuickSetupViewModel.m
//  YiLin
//
//  Created by Zyark on 2018/7/6.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYQuickSetupViewModel.h"
#import "AFNetWorkUtils.h"
#import "ZYModeTypeModel.h"

@interface ZYQuickSetupViewModel()
/**
 新风系统滤网默认值
 */
@property(nonatomic,strong) RACCommand *vsFilterCommand;
/**
 获取空调，地暖的模式默认值
 */
@property(nonatomic,strong) RACCommand *modeDefaultCommand;

/**
 设置模式值
 */
@property(nonatomic,strong) RACCommand *modeModifyCommand;

/**
 获取模式值
 */
@property(nonatomic,strong) RACCommand *modeValueCommand;
@end

@implementation ZYQuickSetupViewModel

-(RACCommand *)vsFilterCommand{
    if(!_vsFilterCommand){
        _vsFilterCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_FILTER_DEFAULT_VALUE_URL];
                NSDictionary *params = @{@"ssid":[ZYConfig loginToken]};
                [[AFNetWorkUtils racGETWithURL:url params:params] subscribeNext:^(id x) {
                    NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
                    NSArray *result = [x objectForKey:@"result"];
                    NSMutableArray *modelArray = [NSMutableArray array];
                    for (NSDictionary *dict in result) {
                        NSString *sysValue = [dict objectForKey:@"value"];
                        NSInteger code = [[dict objectForKey:@"code"] integerValue];
                        ZYModeTypeModel *model = [[ZYModeTypeModel alloc] init];
                        switch (code) {
                            case 5://Primary filter
                                [resultDict setObject:sysValue forKey:[NSString stringWithFormat:@"filtter_%ld",ZYFilterTypeCommon]];
                                break;
                            case 6://PM2.5 filter
                                [resultDict setObject:sysValue forKey:[NSString stringWithFormat:@"filtter_%ld",ZYFilterTypeEfficient]];
                                break;
                            case 7://Fresh air mode
                                model.modeType = ZYVSModeTypeCommon;
                                model.value = sysValue;
                                [modelArray addObject:model];
                                break;
                            case 8://Purification mode
                                model.modeType = ZYVSModeTypePurification;
                                model.value = sysValue;
                                [modelArray addObject:model];
                                break;
                            case 9://Sleep mode
                                model.modeType = ZYVSModeTypeSleeping;
                                model.value = sysValue;
                                [modelArray addObject:model];
                                break;
                            case 10://Mixed mode
                                model.modeType = ZYVSModeTypeMixedWind;
                                model.value = sysValue;
                                [modelArray addObject:model];
                                break;
                            case 11://Automatic mode
                                model.modeType = ZYVSModeTypeAuto;
                                model.value = sysValue;
                                [modelArray addObject:model];
                                break;
                        }
                    }
                    [self makeModeTypeModel:modelArray];
                    [resultDict setObject:modelArray forKey:@"mode"];
                    [subscriber sendNext:resultDict];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _vsFilterCommand;
}

-(RACCommand *)modeDefaultCommand{
    if(!_modeDefaultCommand){
        _modeDefaultCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                NSString *url = [NSString stringWithFormat:@"%@%@%ld",ZYAPI_BASE_URL,ZYAPI_GET_MODE_VALUE_NEW_URL,[input unsignedIntegerValue]];
                NSDictionary *params = @{@"ssid":[ZYConfig loginToken]};
                [[AFNetWorkUtils racGETWithURL:url params:params] subscribeNext:^(id x) {
                    NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
                    NSArray *result = [x objectForKey:@"result"];
                    for (NSDictionary *dict in result) {
                        NSString *sysValue = [dict objectForKey:@"value"];
                        NSInteger code = [[dict objectForKey:@"code"] integerValue];
                        switch (code) {
                            case 1:
                                [resultDict setObject:sysValue forKey:[NSString stringWithFormat:@"%ld",ZYDeviceModeEnergySaving]];
                                break;
                            case 2:
                                [resultDict setObject:sysValue forKey:[NSString stringWithFormat:@"%ld",ZYDeviceModeComfortable]];
                                break;
                            case 3:
                                [resultDict setObject:sysValue forKey:[NSString stringWithFormat:@"%ld",ZYDeviceModeCustom]];
                                break;
                        }
                    }
                    [subscriber sendNext:resultDict];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _modeDefaultCommand;
}

-(RACCommand *)modeModifyCommand{
    if(!_modeModifyCommand){
        @weakify(self);
        _modeModifyCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_MODIFY_MODE_VALUE_URL];
                NSDictionary *params = @{@"ssid":[ZYConfig loginToken],@"macAddress":self.mac,@"type":[input objectForKey:@"type"],@"model":[input objectForKey:@"model"],@"value":[input    objectForKey:@"params"]};
                NSLog(@"params:%@",params);
                [[AFNetWorkUtils racPOSTWithURL:url params:params] subscribeNext:^(id x) {
                    NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
                    [subscriber sendNext:resultDict];
                    [subscriber sendCompleted];
                } error:^(NSError *error) {
                    [subscriber sendError:error];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _modeModifyCommand;
}


-(RACCommand *)modeValueCommand{
    if(!_modeValueCommand){
        _modeValueCommand = [self setupGetModeValueCommand];
    }
    return _modeValueCommand;
}

-(RACCommand *)setupGetModeValueCommand{
    @weakify(self);
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            [self doGetModeValue:subscriber input:input];
            return nil;
        }];
    }];
    return command;
}

-(void)doGetModeValue:(id<RACSubscriber>)subscriber input:(id)input{
    NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_MODE_VALUE_URL];
    NSDictionary *params = @{@"ssid":[ZYConfig loginToken],@"macAddress":self.mac,@"type":input};
    [[AFNetWorkUtils racGETWithURL:url params:params] subscribeNext:^(id x) {
        NSArray *array = [x objectForKey:@"result"];
        NSArray *result = [NSArray modelArrayWithClass:[ZYModeTypeModel class] json:array];
        [self makeModeTypeModel:result];
        [subscriber sendNext:result];
        [subscriber sendCompleted];
    } error:^(NSError *error) {
        [subscriber sendError:error];
        [subscriber sendCompleted];
    }];
}

-(void)makeModeTypeModel:(NSArray *)result{
    for (ZYModeTypeModel *model in result) {
        if(model.modeType == ZYVSModeTypeAuto){
            NSArray *array = [model.value componentsSeparatedByString:@"/"];
            if(array && [array count] >= 3){
                model.autoMinute = [[array objectAtIndex:1] unsignedIntegerValue];
                model.autoWindSpeedType = [[array objectAtIndex:0] unsignedIntegerValue];
                NSString *date = [array objectAtIndex:2];
                NSArray *dateArray = [date componentsSeparatedByString:@"-"];
                int count = 0;
                for (NSString *timeStr in dateArray) {
                    NSArray *timeArray = [timeStr componentsSeparatedByString:@":"];
                    if(count == 0){
                        model.startHour = [timeArray objectAtIndex:0];
                        model.startMinute = [timeArray objectAtIndex:1];
                    }else{
                        model.endHour = [timeArray objectAtIndex:0];
                        model.endMinute = [timeArray objectAtIndex:1];
                    }
                    count ++;
                }
            }else{
                model.autoMinute = 30;
                model.speedType = ZYWindSpeedTypeLow;
                model.startHour = @"23";
                model.startMinute = @"00";
                model.endHour = @"07";
                model.endMinute = @"00";
            }
        }else if(model.modeType == ZYFilterTypeCommon || model.modeType == ZYFilterTypeEfficient){
            model.filterValue = [model.value intValue];
        }else if(model.modeType >= 100 && model.modeType <=103){
            model.isOpen = [model.value boolValue];
        }else{
            if(model.modeType == ZYVSModeTypeMixedWind){
                NSArray *array = [model.value componentsSeparatedByString:@"/"];
                if(array && [array count] >= 1){
                    NSUInteger windSpeedType = [[array objectAtIndex:0] unsignedIntegerValue];
                    int proportion = [[array objectAtIndex:1]  intValue];
                    model.speedType = windSpeedType;
                    model.proportion = proportion;
                }else{
                    model.speedType = ZYWindSpeedTypeLow;
                    model.proportion = 0;
                }
            }else{
                NSUInteger windSpeedType = [model.value unsignedIntegerValue];
                model.speedType = windSpeedType;
            }
        }
    }
}

-(void)setModeValueWithDeviceType:(NSUInteger)type andModel:(NSUInteger)model andParams:(NSString *)params{
    [self.modeModifyCommand execute:@{@"type":[NSNumber numberWithUnsignedInteger:type],@"model":[NSNumber numberWithUnsignedInteger:model],@"params":params}];
}

@end
